/*
 * author      : dormi330
 * date        : 2018/6/28
 * description : 订单controller
 */

package com.deep.trade.adaptor.provided.rest;

import com.deep.trade.application.AppRuntimeEvn;
import com.deep.trade.application.OrderFacadeImpl;
import com.deep.trade.application.exception.LayerControllerException;
import com.deep.trade.common.CommonUtils;
import com.deep.trade.common.HttpResponsePayload;
import com.deep.trade.order.dto.InDtoEachParcel;
import com.deep.trade.order.dto.OutDtoTradeOrder;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
@RequestMapping(value = "/api/v1/order")
public class OrderMgmtController {

    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());

    private AppRuntimeEvn appRuntimeEvn;
    private OrderFacadeImpl orderFacadeImpl;

    private HttpResponseAssembler assembleService;

    @Autowired
    public OrderMgmtController(
            AppRuntimeEvn appRuntimeEvn
            , OrderFacadeImpl orderFacadeImpl
            , HttpResponseAssembler assembleService
    ) {
        this.appRuntimeEvn = appRuntimeEvn;
        this.orderFacadeImpl = orderFacadeImpl;
        this.assembleService = assembleService;
    }

    // ============ 查询 ===========

    /**
     * 卖家查询订单列表, 管理端使用
     */
    @ResponseBody
    @RequestMapping(value = "/mgmt/list-of-seller", method = RequestMethod.GET)
    public HttpResponsePayload tradeOrderListOfSeller(
            @RequestParam(required = false) String token,
            @RequestParam(required = false) String tenantCode,
            @RequestParam(value = "page", required = false, defaultValue = "1") int page,
            @RequestParam(value = "size", required = false, defaultValue = "10") int size
    ) throws Exception {
        if (null == token) throw LayerControllerException.argumentNotFound("token");
        if (null == tenantCode) throw LayerControllerException.argumentNotFound("tenantCode");

        logger.info("{}, tenantCode={}, size={}, pageIndex={}", CommonUtils.currentClassAndMethod(), tenantCode);
        appRuntimeEvn
                .ensureTenantCode(tenantCode)
                .ensureToken(token);

        // TODO token 目前按照 Tenant订单处理
        PageInfo<OutDtoTradeOrder> pageInfo = orderFacadeImpl.tradeOrderListOfTenant(page, size);
        return assembleService.happy(pageInfo);
    }

    /**
     * 平台查询订单列表, 管理端使用
     */
    @ResponseBody
    @RequestMapping(value = "/mgmt/list-of-platform", method = RequestMethod.GET)
    public HttpResponsePayload tradeOrderListOfTenant(
            @RequestParam(required = false) String token,
            @RequestParam(required = false) String tenantCode,
            @RequestParam(value = "page", required = false, defaultValue = "1") int page,
            @RequestParam(value = "size", required = false, defaultValue = "10") int size
    ) throws Exception {
        if (null == token) throw LayerControllerException.argumentNotFound("token");
        if (null == tenantCode) throw LayerControllerException.argumentNotFound("tenantCode");

        logger.info("{}, tenantCode={}, size={}, pageIndex={}", CommonUtils.currentClassAndMethod(), tenantCode);
        appRuntimeEvn
                .ensureTenantCode(tenantCode)
                .ensureToken(token);

        // TODO token
        PageInfo<OutDtoTradeOrder> pageInfo = orderFacadeImpl.tradeOrderListOfTenant(page, size);
        return assembleService.happy(pageInfo);
    }

    // ============ 查询 ===========

    /**
     * 为订单添加包裹, 管理端使用
     */
    @ResponseBody
    @RequestMapping(value = "/mgmt/parcel", method = RequestMethod.POST)
    public HttpResponsePayload addParcelOfTrade(
            @RequestParam(required = false) String token,
            @RequestParam(required = false) String tenantCode,
            @RequestBody List<InDtoEachParcel> inDtoEachParcelList
    ) throws Exception {
        if (null == token) throw LayerControllerException.argumentNotFound("token");
        if (null == tenantCode) throw LayerControllerException.argumentNotFound("tenantCode");

        logger.info("{}, tenantCode={}, size={}, pageIndex={}", CommonUtils.currentClassAndMethod(), tenantCode);
        appRuntimeEvn
                .ensureTenantCode(tenantCode)
                .ensureToken(token);

        orderFacadeImpl.addParcel(inDtoEachParcelList);
        return assembleService.happy();
    }
}
